package com.sciencep.pro.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sciencep.pro.common.domain.OscaConstant;
import com.sciencep.pro.common.domain.QueryRequest;
import com.sciencep.pro.common.utils.SortUtil;
import com.sciencep.pro.core.dao.UserMapper;
import com.sciencep.pro.core.domain.dto.home.NewyAssignedVO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.domain.dto.user.UserListRequestTO;
import com.sciencep.pro.core.domain.dto.user.UserListResponseTO;
import com.sciencep.pro.core.domain.entity.Role;
import com.sciencep.pro.core.domain.entity.User;
import com.sciencep.pro.core.service.IRoleService;
import com.sciencep.pro.core.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Sciencep
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    IRoleService roleService;

    @Override
    public Page<UserListResponseTO> getUserList(QueryRequest queryRequest, UserListRequestTO userListRequestTO) {

        Page<UserListResponseTO> page = new Page<>();
        SortUtil.handlePageSort(queryRequest, page, "hiredDate", OscaConstant.ORDER_DESC, false);
        Page<UserListResponseTO> result = baseMapper.getUserList(page, userListRequestTO);
        Map<String,String> roleMap  = roleService.list(new LambdaQueryWrapper<Role>().orderByDesc(Role::getId)).stream().
                collect(Collectors.toMap(Role::getRoleId, Role::getRoleName));
        if (CollUtil.isNotEmpty(result.getRecords())) {
            for (UserListResponseTO o : result.getRecords()) {
                List<Role> roleList = new ArrayList<>();
                if (StringUtils.isNotBlank(o.getRoldIds()) && StringUtils.isNotBlank(o.getRoleNames())) {
                    String[] roIds = o.getRoldIds().split(",");
                        for(String r : roIds){
                            Role role =new Role();
                            role.setRoleId(r).setRoleName(roleMap.get(r));
                            roleList.add(role);
                        }
                        o.setRoleList(roleList);
                    }
                }
        }
        return result;
    }

    @Override
    public UserDetail getUserDetailByUserId(String userId) {

        UserDetail userDetail = baseMapper.getUserDetail(userId);
        if (userDetail != null) {
            userDetail.setButtonIds(baseMapper.getButtonIdsByUserId(userId));
        }


        return userDetail;
    }

    @Override
    public boolean updateUser(User user) {
        return baseMapper.updateUser(user);
    }

    @Override
    public List<UserListResponseTO> getUserListByDeptId(Long deptId) {
        return baseMapper.getUserListByDeptId(deptId);
    }

    @Override
    public NewyAssignedVO getNewyAssignedVO(String userId) {


        return null;
    }

    @Override
    public List<User> getUsers() {
        return baseMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getIsDel,0));

    }
}
